{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelzoo.modelzoo import ModelZoo\n",
    "from modelzoo.metainfo import ModelMeta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-04-10 15:11:37,946 - modelscope - INFO - Development mode use revision: master\n"
     ]
    }
   ],
   "source": [
    "# Global constant\n",
    "MODELSCOPE_API = \"3ceb1149-f13a-42f0-bfaa-4932eca2a9a5\"\n",
    "# OSS_CONFIG = \"/Users/thekernelz/Code/my_test/modelzoo/oss.config\"\n",
    "OSS_CONFIG = None\n",
    "\n",
    "modelzoo = ModelZoo(modelscope_api=MODELSCOPE_API, modelzoo_name=\"pai\", oss_config_file=OSS_CONFIG)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-04-10 13:56:48,598 - modelscope - INFO - Model revision not specified, use default: master in development mode\n",
      "2023-04-10 13:56:48,599 - modelscope - INFO - Development mode use revision: master\n",
      "2023-04-10 13:56:48,888 - modelscope - ERROR - Response details: b'{\"Code\":10010205001,\"Message\":\"\\xe8\\x8e\\xb7\\xe5\\x8f\\x96\\xe6\\xa8\\xa1\\xe5\\x9e\\x8b\\xe4\\xbf\\xa1\\xe6\\x81\\xaf\\xe5\\xa4\\xb1\\xe8\\xb4\\xa5\\xef\\xbc\\x8c\\xe4\\xbf\\xa1\\xe6\\x81\\xaf\\xef\\xbc\\x9arecord not found\",\"RequestId\":\"9e2bc9e6-d766-4f57-b430-6683b9387f0f\",\"Success\":false}'\n",
      "ERROR:modelscope:Response details: b'{\"Code\":10010205001,\"Message\":\"\\xe8\\x8e\\xb7\\xe5\\x8f\\x96\\xe6\\xa8\\xa1\\xe5\\x9e\\x8b\\xe4\\xbf\\xa1\\xe6\\x81\\xaf\\xe5\\xa4\\xb1\\xe8\\xb4\\xa5\\xef\\xbc\\x8c\\xe4\\xbf\\xa1\\xe6\\x81\\xaf\\xef\\xbc\\x9arecord not found\",\"RequestId\":\"9e2bc9e6-d766-4f57-b430-6683b9387f0f\",\"Success\":false}'\n",
      "2023-04-10 13:56:48,890 - modelscope - INFO - Create new model TheKernelZ/test_model_111\n",
      "2023-04-10 13:56:53,727 - modelscope - INFO - [master ebbadd1] 'upload model'\n",
      " 2 files changed, 1 insertion(+), 9 deletions(-)\n",
      " delete mode 100644 README.md\n",
      " create mode 100644 configuration.json\n",
      "\n",
      "2023-04-10 13:57:01,548 - modelscope - INFO - [master 899b7e1] 'upload model'\n",
      " 1 file changed, 0 insertions(+), 0 deletions(-)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Your own model\n",
    "# model_dir = \"your model dir\"\n",
    "model_dir = \"/Users/thekernelz/.cache/modelzoo/test_model_111\"\n",
    "model_name = \"test_model_111\"\n",
    "model_description = \"\"\"\n",
    "high quality anime style model.\n",
    "\n",
    "more info. https://huggingface.co/gsdf/Counterfeit-V2.0\n",
    "\n",
    "Verson2.5 https://huggingface.co/gsdf/Counterfeit-V2.5\n",
    "EasyNegative(Negative Embedding) https://huggingface.co/datasets/gsdf/EasyNegative\n",
    "\"\"\"\n",
    "model_tags = [\"girl\"]\n",
    "\n",
    "# Create model\n",
    "model = modelzoo.create_model(model_dir=model_dir, model_name=model_name, model_description=model_description, model_tags=model_tags)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Update Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-04-10 13:58:49,859 - modelscope - INFO - Model revision not specified, use default: master in development mode\n",
      "2023-04-10 13:58:49,860 - modelscope - INFO - Development mode use revision: master\n",
      "Downloading: 100%|██████████| 119k/119k [00:00<00:00, 1.15MB/s]\n",
      "2023-04-10 13:58:52,556 - modelscope - INFO - [master 2058f1e] '20230410-135850'\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 test.py\n",
      "\n",
      "2023-04-10 13:58:58,476 - modelscope - INFO - [master c7dacbb] 'upload model'\n",
      " 1 file changed, 2 insertions(+), 2 deletions(-)\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"The model 'test_model_111' has been successfully modified on ModelScope and updated in the local model zoo.\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Your updated model\n",
    "model_dir = \"/Users/thekernelz/.cache/modelzoo/test_model_111\"\n",
    "model_name = \"test_model_111\"\n",
    "version = None  # updated version\n",
    "description = None  # updated description\n",
    "\n",
    "# Update model\n",
    "modelzoo.modify_model(model_dir=model_dir, model_name=model_name, version=version, description=description)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your wanted model\n",
    "model_name = \"list_model_14\"\n",
    "output_dir = None  # customed output_dir\n",
    "\n",
    "modelzoo.download_model(model_name=model_name, output_dir=output_dir)  # download all the model in modelzoo"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Delete Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-04-10 13:59:55,880 - modelscope - INFO - Model revision not specified, use default: master in development mode\n",
      "2023-04-10 13:59:55,880 - modelscope - INFO - Development mode use revision: master\n",
      "Downloading: 100%|██████████| 112k/112k [00:00<00:00, 1.10MB/s]\n",
      "2023-04-10 13:59:59,862 - modelscope - INFO - [master b0ede44] 'upload model'\n",
      " 1 file changed, 2 insertions(+), 2 deletions(-)\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"The model 'DreamShaper' has been successfully deleted.\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_name = \"DreamShaper\"\n",
    "\n",
    "modelzoo.delete_model(model_name=model_name)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show with Search"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List all models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total num:\n",
      "\t9\n",
      "models: \n",
      "\tstLouisLuxuriousWheels_v1\n",
      "\tkoreandolllikeness_V10\n",
      "\tneverendingDreamNED\n",
      "\trealisticVisionV20_v20\n",
      "\t3Guofeng3_v32Light\n",
      "\tdeliberate_v2\n",
      "\tmidjourney_large-finetune\n",
      "\ttest_model_123\n",
      "\ttest_model_1234\n"
     ]
    }
   ],
   "source": [
    "models = modelzoo.list_model()\n",
    "\n",
    "print(\"total num:\")\n",
    "print(\"\\t\" + str(len(models)))\n",
    "print(\"models: \")\n",
    "for model in models:\n",
    "    print(\"\\t\" + model)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Search "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = \"midjourney_large-finetune\"\n",
    "model_tag = \"your\"\n",
    "topk = 1  # model nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dummystart\n",
      "<Document ('id', 'parent_id', 'granularity', 'tags', '_metadata', 'chunks') at fd7e00105d38ca0f8be4123cdc8579fc>\n",
      "dummyend\n",
      "<Document ('id', 'parent_id', 'granularity', 'tags', '_metadata', 'chunks') at 5775778c31470d452176b48e6294f62f>\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-04-10 15:12:41,360 - modelscope - INFO - Nothing to commit.\n",
      "2023-04-10 15:12:42,118 - modelscope - INFO - [master a61556c] 'upload model'\n",
      " 1 file changed, 2 insertions(+), 2 deletions(-)\n",
      "\n",
      "2023-04-10 15:13:22,024 - modelscope - INFO - Nothing to commit.\n"
     ]
    }
   ],
   "source": [
    "## Get Single Model by Model Name\n",
    "model = modelzoo.get_model_by_name(\"midjourney_large-finetune\")\n",
    "for h in model.history_records:\n",
    "    print(h.prompt.text)\n",
    "    print(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'your' does not exists in any model of modelzoo.\n"
     ]
    }
   ],
   "source": [
    "## Get Models by Model Tag\n",
    "models = modelzoo.get_model_by_tag(model_tag)\n",
    "if models is not None:\n",
    "    for model in models:\n",
    "        ModelMeta(model).to_table()\n",
    "else:\n",
    "    print(\"'{}' does not exists in any model of modelzoo.\".format(model_tag))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================================================================================================\n",
      "model_name              | deliberate_v2\n",
      "model_description       | All in One / Any Case Version\n",
      "This model provides you the ability to create anything you want. The more power of prompt knowledges you have, the better results you'll get. It basically means that you'll never get a perfect result with just a few words. You have to fill out your prompt line extremely detailed.\n",
      "\n",
      "Who find this model perfect:\n",
      "NSFW masters\n",
      "\n",
      "Meticulous anatomy artists\n",
      "\n",
      "Creative prompters\n",
      "\n",
      "Art designers\n",
      "\n",
      "Dive into the perfect creations world with my prompts. Your research will be appreciated, so feel free to show everyone, what you can get with this model.\n",
      "\n",
      "model_created           | 20230407-174311\n",
      "model_updated           | 20230407-174311\n",
      "model_version           | initial\n",
      "model_author            | model_author_test\n",
      "model_dataset           | model_dataset_default\n",
      "model_default_sampler   | model_default_sampler_test\n",
      "model_default_cfg_scale | 0.7\n",
      "model_default_steps     | 20\n",
      "model_file_list         | deliberate_v2.safetensors, configuration.json\n",
      "model_tags              | safetensors, SD1.5, author_name_TheKernelZ, modelzoo_name_pai\n",
      "history_records         | Prompt(prompt='dummystart', output_image=None, negative_prompt='', sampler='', model='', seed='', cfg_scale=0.7, steps=20, prompt_keys='', negative_prompt_keys='', identity='Prompt'), Prompt(prompt='dummyend', output_image=None, negative_prompt='', sampler='', model='', seed='', cfg_scale=0.7, steps=20, prompt_keys='', negative_prompt_keys='', identity='Prompt'), Prompt(prompt='a photo of an astronaut riding a horse on earth 111', output_image='oss://pai-vision-exp/wzh-zhoulou/aigc/models/deliberate_v2/SUHYsBip4b.png', negative_prompt='less figure', sampler='PNDMScheduler', model='deliberate_v2', seed=1231321124123, cfg_scale=7.5, steps=50, prompt_keys='', negative_prompt_keys='', identity='Prompt')\n"
     ]
    }
   ],
   "source": [
    "## Search Models by Model Name\n",
    "models = modelzoo.search_model_by_name(model_name, topk=topk)\n",
    "if models is not None:\n",
    "    for model in models:\n",
    "        ModelMeta(model).to_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================================================================================================\n",
      "model_name              | stLouisLuxuriousWheels_v1\n",
      "model_description       | \n",
      "About this version\n",
      "\n",
      "hair ornament for \"YS\" (it's like default anyways)\n",
      "\n",
      "silver dress + revealing clothes for silver dress\n",
      "\n",
      "necklace for jewelry necklace\n",
      "\n",
      "earrings for silver earrings\n",
      "\n",
      "high heels for silver high heels (it's like default anyways)\n",
      "\n",
      "handbag for black handbag\n",
      "\n",
      "blue nails for blue fingernails (sometimes toenails too)\n",
      "\n",
      "champagne flute/cup tags should work ok\n",
      "\n",
      "I tried way too many times for the entire skin to look accurate but it's way too many details for the AI to handle at this point :(\n",
      "\n",
      "LoRA issues:\n",
      "\n",
      "This LoRA is quite overfitted by the dataset, you can try to tag against it such as barefoot to get rid of high heels, car in negative to remove car ish things, necklace in negative to remove necklace ish things etc.\n",
      "\n",
      "Might not always work, just generate more if so :/\n",
      "\n",
      "All previews in v1 (epoch5) are made with AOM2_Hard and most of them with kl-f8-anime2 vae. Some were with 840000 vae but I think it's inferior to kl-f8-anime2 now.\n",
      "\n",
      "(Never mind there's 1 image generated with epoch3, but after tests I think epoch5 is best)\n",
      "\n",
      "Good luck have fun ( ͡° ͜ʖ ͡°)\n",
      "\n",
      "model_created           | 20230407-141130\n",
      "model_updated           | 20230407-141130\n",
      "model_version           | initial\n",
      "model_author            | model_author_test\n",
      "model_dataset           | model_dataset_default\n",
      "model_default_sampler   | model_default_sampler_test\n",
      "model_default_cfg_scale | 0.7\n",
      "model_default_steps     | 20\n",
      "model_file_list         | stLouisLuxuriousWheels_v1.safetensors, configuration.json\n",
      "model_tags              | LORA, st. louis, (luxurious wheels), (azur lane, author_name_TheKernelZ, modelzoo_name_pai\n",
      "history_records         | Prompt(prompt='dummystart', output_image=None, negative_prompt='', sampler='', model='', seed='', cfg_scale=0.7, steps=20, prompt_keys='', negative_prompt_keys='', identity='Prompt'), Prompt(prompt='dummyend', output_image=None, negative_prompt='', sampler='', model='', seed='', cfg_scale=0.7, steps=20, prompt_keys='', negative_prompt_keys='', identity='Prompt')\n"
     ]
    }
   ],
   "source": [
    "models = modelzoo.search_model_by_tag(model_tag, topk=topk)\n",
    "if models is not None:\n",
    "    for model in models:\n",
    "        ModelMeta(model).to_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from diffusers import StableDiffusionPipeline\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_id = \"runwayml/stable-diffusion-v1-5\"\n",
    "pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)\n",
    "pipe = pipe.to(\"cuda\")\n",
    "\n",
    "prompt = \"a photo of an astronaut riding a horse on mars\"\n",
    "\n",
    "# In principle, you can play any parameter of the pipeline\n",
    "result = modelzoo.insert_image(pipe, \"midjourney_large-finetune\", oss_dir=\"wzh-zhoulou/aigc/models/\", prompt=prompt, negative_prompt=\"less figure\", seed=1231321124123)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "modelzoo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
